package org.chameleon.threadpool;


import java.util.concurrent.ThreadPoolExecutor;

public class ThreadPoolMonitorData {

    private final ThreadPoolExecutor threadPoolExecutor;
    public ThreadPoolMonitorData(ThreadPoolExecutor threadPoolExecutor ) {
        this.threadPoolExecutor= threadPoolExecutor;
        setCompletedTaskCount(threadPoolExecutor.getCompletedTaskCount());
        setLargestPoolSize(threadPoolExecutor.getLargestPoolSize());
        setCorePoolSize(threadPoolExecutor.getCorePoolSize());
        setMaximumPoolSize(threadPoolExecutor.getMaximumPoolSize());
        setCompletedTaskCount(threadPoolExecutor.getCompletedTaskCount());

    }

    /**
     * Tracks largest attained pool size. Accessed only under
     * mainLock.
     */
    private int largestPoolSize;

    /**
     * Counter for completed tasks. Updated only on termination of
     * worker threads. Accessed only under mainLock.
     */
    private long completedTaskCount;

    /**
     * Timeout in nanoseconds for idle threads waiting for work.
     * Threads use this timeout when there are more than corePoolSize
     * present or if allowCoreThreadTimeOut. Otherwise they wait
     * forever for new work.
     */
    private volatile long keepAliveTime;

    /**
     * If false (default), core threads stay alive even when idle.
     * If true, core threads use keepAliveTime to time out waiting
     * for work.
     */
    private volatile boolean allowCoreThreadTimeOut;

    /**
     * Core pool size is the minimum number of workers to keep alive
     * (and not allow to time out etc) unless allowCoreThreadTimeOut
     * is set, in which case the minimum is zero.
     *
     * Since the worker count is actually stored in COUNT_BITS bits,
     * the effective limit is {@code corePoolSize & COUNT_MASK}.
     */
    private volatile int corePoolSize;

    /**
     * Maximum pool size.
     *
     * Since the worker count is actually stored in COUNT_BITS bits,
     * the effective limit is {@code maximumPoolSize & COUNT_MASK}.
     */
    private volatile int maximumPoolSize;


    public ThreadPoolMonitorData setLargestPoolSize(int largestPoolSize) {
        this.largestPoolSize = largestPoolSize;
        return this;
    }

    public long getCompletedTaskCount() {
        return completedTaskCount;
    }

    public ThreadPoolMonitorData setCompletedTaskCount(long completedTaskCount) {
        this.completedTaskCount = completedTaskCount;
        return this;
    }

    public long getKeepAliveTime() {
        return keepAliveTime;
    }

    public ThreadPoolMonitorData setKeepAliveTime(long keepAliveTime) {
        this.keepAliveTime = keepAliveTime;
        return this;
    }

    public boolean isAllowCoreThreadTimeOut() {
        return allowCoreThreadTimeOut;
    }

    public ThreadPoolMonitorData setAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut) {
        this.allowCoreThreadTimeOut = allowCoreThreadTimeOut;
        return this;
    }

    public int getCorePoolSize() {
        return corePoolSize;
    }

    public ThreadPoolMonitorData setCorePoolSize(int corePoolSize) {
        this.corePoolSize = corePoolSize;
        return this;
    }

    public int getMaximumPoolSize() {
        return maximumPoolSize;
    }

    public ThreadPoolMonitorData setMaximumPoolSize(int maximumPoolSize) {
        this.maximumPoolSize = maximumPoolSize;
        return this;
    }
}
